package member

import (
	"dhfshop/models/member"
	"time"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
)

type MemberLevelController struct {
	base.BaseController
}

func (this *MemberLevelController) Get() {
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	list, cnt, err := member.GetAllMemberLevels(partnerId,query,fields,order_by,offset,limit)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	r := struct {
		TotalCount int64               `json:"total_count"`
		Levels   []*member.MemberLevel `json:"member_levels"`
	}{TotalCount: cnt, Levels: list}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *MemberLevelController) Post() {
	var v member.MemberLevel
	valid := validation.Validation{}
	this.ParseForm(&v)
	valid.Required(v.Name, "name")
	valid.Required(v.Value, "value")
	valid.Required(v.RequireExp, "require_exp")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	v.PartnerId =int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if _, err := member.AddMemberLevel(&v); err != nil {
        this.ServeError(err.Error())
		return
    }
	rsp := struct {
		MemberLevelId  int64      `json:"member_level_id"`
	    Created        time.Time  `json:"created"`
	}{MemberLevelId: v.MemberLevelId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *MemberLevelController) Put() {
	var v member.MemberLevel
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.MemberLevelId, "member_level_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{"member_level_id"})
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(updateFields) > 0 {
		_, err = member.UpdateMemberLevel(&v, updateFields...)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		MemberLevelId  int64       `json:"member_level_id"`
	    Modified  time.Time   `json:"modified"`
	}{MemberLevelId:v.MemberLevelId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}


func (this *MemberLevelController) Delete() {
	var v member.MemberLevel
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.MemberLevelId, "member_level_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	err := member.DeleteMemberLevel(partnerId, v.MemberLevelId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		MemberLevelId int64     `json:"member_level_id"`
		Modified time.Time `json:"modified"`
	}{MemberLevelId: v.MemberLevelId, Modified: time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}


